<%@page import="java.sql.ResultSet"%>
<%@ page import="java.util.Date" language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page session="true" %>
<%@ page contentType="text/html; charset=UTF-8" import="com.mysql.jdbc.*"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.Connection"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.DriverManager"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.Statement"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.util.*"%>

<%!
	public String getProjectList(String _strUser){
		Connection con = null;
	    Statement stmt = null;
	    String strRet = new String();
	 	int ret = 0;
	 	
	    try
	    {
	    	Class.forName("com.mysql.jdbc.Driver").newInstance(); 
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mimo" ,"root" ,"d0o9o84");
	        
	        stmt = conn.createStatement();
	        
	        ResultSet result = stmt.executeQuery("select * from projectrels where userId='" + _strUser + "';");
	        
	        List<Integer> projectList = new ArrayList<Integer>();
	        
	        while(result.next()){
	        	Integer nProjectId = result.getInt("projectId");
	        	
	        	projectList.add(nProjectId);
	        }
	        
	        result.close();
	        
	        Iterator<Integer> i = projectList.iterator();
	        strRet = "<projectList count='"+ projectList.size() +"'>";
	        while(i.hasNext()){
	        	result = stmt.executeQuery("select * from projectattribute where projectId='" + i + "';");
	        	
	        	strRet += "<project>";
	        	strRet += "</project>";
	        	
	        	i.next();
	        }
	        
	        strRet += "</projectList>";
	        
	        result.close();
	        stmt.close();
	        conn.close();
	    }
	    catch( Exception ex )
	    {
	    	ex.printStackTrace();
	    	ret = -1;
	    }
	
		return ""; 
	}
%>
<%
	session = request.getSession(true);
	
	String strId = (String)session.getAttribute("id");
	
	if(strId == null){
		response.sendRedirect("login.jsp");
	}

	String strCmd = request.getParameter("cmd");
	
	int bLoginPage = 0;
	
	if((strCmd != null) && (strCmd.equals("getProjectList") == true)){
		int ret = 0;
		String strResponse = getProjectList(strId);
		response.setContentType("text/xml");
		
		if(ret == 0){
			out.println(strResponse);
			return;
		}else{
			out.println(strResponse);
			return;
		}
	}
%>